/**
 * Player
 * Template of player entity
 **/
var Player = function() {

// CONFIGURATION ---
	this.sprite = undefined;
	this.sprites = [];

	this.state = 'spawn';

	this.direction = -1;

	this.life = 1000;
// --- END CONFIGURATION

// CORE ---
	this.init = function() {
		log('*** INIT PLAYER ***');

		// Initialize sprites
		var conf = PlayerConf.sprites;
		for(cName in conf) {
			// Save sprite instance and animation linked
			this.sprites[cName] = conf[cName].img;
		}

		// Place player outside the canvas
		this.x =  - PlayerConf.bounds.w; 
		this.y = PlayerConf.coords.y;
	}

	this.update = function() {
		switch(this.state) {
			// Spawn action
			case 'spawn' : 
				this.spawn();
			break;
			// Leave action
			case 'leave' : 
				this.leave();
			break;
			// Die action
			case 'die' :
				this.die();
			break;
		}

		this.animate();
	}

	this.animate = function() {

		// Default animation (idle)
		var sprite = 'idle';

		switch(this.state) {
			// Run animation for spawning
			case 'spawn' : 
			case 'leave' : 
				sprite = 'walk';
			break;
			// Animation of strike on player
			case 'strike' :
				sprite = 'strike';
				this.state = 'striked';
			break;
			// Animation of death
			case 'die' :
				sprite = 'die';
			break;
			// Default animation (idle)
			default : 
				sprite = 'idle';
			break;
		}

		if(typeof this.sprite !== "undefined" && this.sprite.name !== sprite) {
			this.sprite = IM.killInstance(this.sprite);
		} 
		
		if(typeof this.sprite === "undefined") {
			this.sprite = IM.getInstance(getIMName(this.sprites[sprite]));
			this.sprite.animation = new IIG.Animation(PlayerConf.sprites[sprite].animation);
			this.sprite.name = sprite;
		}
	}

	this.render = function() {
		// Save current context
		CTX.save();
		// Scale according to player's direction
		CTX.scale(this.direction, 1);
		// Render sprites in the appropriate direction
		IM.drawImage(CTX, this.sprite, this.x * this.direction, this.y);
		// Restore context
		CTX.restore();

		if(PlayerConf.debug === true) {
			this.debug();
		}
	}
// --- END CORE

// ACTIONS --- 
	this.spawn = function() {
		if(this.x < PlayerConf.coords.x) {
			this.x += PlayerConf.speed;
		} else {
			this.idle();
		}
	}

	this.leave = function() {
		if(this.x < WIDTH + PlayerConf.bounds.w) {
			this.x += PlayerConf.speed;
		} else {
			this.idle();
		}
	}

	this.strike = function() {
		this.state = 'strike';
		this.life--;

		// Trigger die action
		if(this.life <= 0) {
			this.state = 'die';
		}
	}

	this.idle = function() {
		this.state = 'idle';
	}

	this.die = function() {
		this.state = 'die';
		if(this.sprite.animationDestroyed === true) {
			log('*** GAME OVER ***');
			game.mScore.save();
			game.mPlayer.deleteOne(this);
			currentGameObject.cleared = true;
			displayMenu();
		}
	}
// --- END ACTIONS

// UTILS ---
	this.getX = function() {
		if(this.direction === -1) {
			return this.x - PlayerConf.bounds.w;
		}
		else {
			return this.x;
		}
	}

	this.debug = function() {
		CTX.fillStyle = "rgba(0,0,255,.3)";
		CTX.fillRect(this.getX(), this.y, PlayerConf.bounds.w, PlayerConf.bounds.h);
	}
// --- END UTILS

};